package cn.shengongying.api.dto;

import cn.shengongying.bean.BaseUser;
import cn.shengongying.exception.ParamException;
import cn.shengongying.model.BossCompanyInfo;
import cn.shengongying.model.BossPosition;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.collections4.CollectionUtils;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;

/**
 * author: RXK
 * date: 2022/1/7 星期五
 * desc:
 */
@Getter
@Setter
@ApiModel("职位入参")
public class PositionInDTO {


    @ApiModelProperty(value = "id")
    private Long id;

    @ApiModelProperty(value = "职位名称")
    @NotEmpty(message = "职位不能为空")
    private String positionName;


    @ApiModelProperty(value = "类型(0:临工;1:兼职;2:全职)")
    @NotNull(message = "类型不能为空")
    private Integer type;

    @ApiModelProperty(value = "招聘人数/用工人数")
    @NotNull(message = "人数不能为空")
    @Min(value = 1,message = "人数至少为1人")
    private Integer peopleNum;

    @ApiModelProperty(value = "开工时间(兼职、临工(yyyy-MM-dd))")
    @JsonFormat(pattern = "yyyy-MM-dd")
    private Date startTime;

    /**
     * 结束时间
     */
    @ApiModelProperty(value = "结束时间(兼职、临工)")
    @JsonFormat(pattern = "yyyy-MM-dd")
    private Date endTime;

    /**
     * 班别类型(0:白班;1:夜班)
     */
    @ApiModelProperty(value = "班别类型(0:白班;1:夜班)(时间戳)")
    private Integer workTimeType;

    /**
     * 薪资类型(-1:面议;0:小时;1:天;2:按产品报价;3:月)
     */
    @ApiModelProperty(value = "薪资类型((-1:面议;0:小时;1:天;2:按产品报价;3:月))")
    @NotNull(message = "薪资类型不能为空")
    private Integer salaryType;

    /**
     * 薪水(x元/小时、x元/小时)
     */
    @ApiModelProperty(value = "(最小/最低)薪资金额(xxxx元)")
    @NotNull(message = "薪资不能为空")
    private Integer salary;

    /**
     * 最大薪水
     */
    @ApiModelProperty(value = "最大薪资金额")
    private Integer maxSalary;

    /**
     * 工作时长
     */
    @ApiModelProperty(value = "工作时长")
    private Integer workHour;

    /**
     * 福利类型(0:包吃住;1:包住不包吃;3:包吃不包住;4:有夜宵;5:包住有餐补;6:包吃有房补)(2,3,4,5多选类型)
     */
    @ApiModelProperty(value = "福利类型(0:包吃住;1:包住不包吃;3:包吃不包住;4:有夜宵;5:包住有餐补;6:包吃有房补)(2,3,4,5逗号分割多选类型)")
    private List<String> benefits;

    /**
     * 性别(0:男;1:女)
     */
    @ApiModelProperty(value = "性别(0:男;1:女)")
    private Integer sex;

    /**
     * 年龄最小值
     */
    @ApiModelProperty(value = "年龄最小值")
    private Integer minAge;

    /**
     * 年龄最大值
     */
    @ApiModelProperty(value = "年龄最大值")
    private Integer maxAge;

    /**
     * 教育背景(0:高中;1:大专;2:本科)
     */
    @ApiModelProperty(value = "教育背景(0:高中;1:大专;2:本科)")
    private Integer educationType;

    /**
     * 任职要求
     */
    @ApiModelProperty(value = "任职要求")
    @NotEmpty(message = "任职要求不能为空")
    private String jobRequirement;

    /**
     * 岗位职责
     */
    @ApiModelProperty(value = "岗位职责")
    @NotEmpty(message = "岗位职责不能为空")
    private String jobResponsibility;

    /**
     * 补贴金额
     */
    @ApiModelProperty(value = "补贴金额")
    private Integer subsidy;

    /**
     * 工资结算类型(0:日结;1:周结;2:月结;3:洗手拿钱)
     */
    @ApiModelProperty(value = "工资结算类型(0:日结;1:周结;2:月结;3:洗手拿钱)")
    private Integer settleAccountType;

    /**
     * 兼职工期类型(1:短期;2:长期)
     */
    @ApiModelProperty(value = "兼职工期类型(0:短期;1:长期)")
    private Integer partTimeType;

    /**
     * 工期(天)
     */
    @ApiModelProperty(value = "工期(天)")
    private Integer workDays;

    /**
     * 其他信息/备注
     */
    @ApiModelProperty(value = "其他信息/备注")
    private String remark;

    /**
     * 照片信息(多张照片按照逗号分割)
     */
    @ApiModelProperty(value = "照片信息(多张照片按照逗号分割)")
    private String picture;

    /**
     * 视频信息(多个视频按照视频分割)
     */
    @ApiModelProperty(value = "视频信息(多个视频按照视频分割)")
    private String video;


    /**
     * 公司名称
     */
    @ApiModelProperty(value = "公司名称")
    @NotEmpty(message = "公司名称不能为空")
    private String companyName;


    @ApiModelProperty(value = "省")
    @NotEmpty(message = "省份不能为空")
    private String province;

    @ApiModelProperty(value = "市")
    @NotEmpty(message = "市不能为空")
    private String city;

    @ApiModelProperty(value = "区")
    @NotEmpty(message = "区不能为空")
    private String region;


    /**
     * 工作地点
     */
    @ApiModelProperty(value = "工作地点")
    @NotEmpty(message = "工作地点不能为空")
    private String address;

    /**
     * 联系人
     */
    @ApiModelProperty(value = "联系人")
    @NotEmpty(message = "联系人不能为空")
    private String linkMan;

    /**
     * 联系电话
     */
    @ApiModelProperty(value = "联系电话")
    @NotEmpty(message = "联系电话不能为空")
    private String linkMobile;

    /**
     * 微信号
     */
    @ApiModelProperty(value = "微信号")
    private String wechatNo;

    public void checkParams() {
        switch (type) {
            case 0:
                shortTimeJobParamCheck();
                break;
            case 1:
                partTimeJobParamCheck();
                break;
            case 2:
                fullTimeJobParamCheck();
                break;
            default:
                throw new ParamException();
        }
    }

    private void partTimeJobParamCheck() {
        if (null == partTimeType || null == workDays || null == startTime || null == endTime) {
            throw new ParamException();
        }
    }

    private void shortTimeJobParamCheck() {
        if (null == workTimeType || CollectionUtils.isEmpty(benefits) || null == subsidy || null == workDays || null == startTime || null == endTime) {
            throw new ParamException();
        }
    }

    private void fullTimeJobParamCheck() {
        if (null == workHour || null == maxSalary || CollectionUtils.isEmpty(benefits) || null == sex || null == minAge || null == maxAge || null == educationType) {
            throw new ParamException();
        }
    }

    public BossPosition build(BaseUser baseUser) {
        BossPosition position = new BossPosition();
        position.setId(id);
        if (null == id) {
            position.setPositionName(this.positionName);
            position.setPeopleNum(this.peopleNum);
        }
        setPositionParam(position,baseUser);
        position.setCompanyName(this.companyName);
        position.setLinkMan(this.linkMan);
        position.setLinkMobile(this.linkMobile);
        position.setAddress(this.address);
        position.setWechat(this.wechatNo);
        position.setProvince(this.province);
        position.setCity(this.city);
        position.setRegion(this.region);
        return position;
    }

    private void setPartTimeJobParam(BossPosition template) {
        template.setMaxSalary(0);
        template.setPartTimeType(this.partTimeType);
        template.setStartTime(this.startTime);
        template.setEndTime(this.endTime);
        template.setWorkDays(workDays);


        template.setWorkHour(0);
        template.setSex(0);
        template.setMinAge(0);
        template.setMaxAge(0);
        template.setEducationType(0);
        template.setPartTimeType(0);
    }

    private void setShortTimeJobParam(BossPosition template) {
        template.setMaxSalary(0);
        template.setWorkTimeType(this.workTimeType);
        String benefits = String.join(",", this.benefits);
        template.setBenefits(benefits);
        template.setSubsidy(this.subsidy);
        template.setStartTime(this.startTime);
        template.setEndTime(this.endTime);
        template.setWorkDays(this.workDays);

        template.setWorkHour(0);
        template.setSex(0);
        template.setMinAge(0);
        template.setMaxAge(0);
        template.setEducationType(0);
        template.setPartTimeType(0);

    }

    private void setFullTimeJobParam(BossPosition template) {
        template.setMaxSalary(this.maxSalary);
        template.setWorkHour(this.workHour);
        String benefits = String.join(",", this.benefits);
        template.setBenefits(benefits);
        template.setSex(this.sex);
        template.setMinAge(this.minAge);
        template.setMaxAge(this.maxAge);
        template.setEducationType(this.educationType);
        template.setStartTime(this.startTime);
        template.setEndTime(this.endTime);


        /*多余的*/
        template.setWorkTimeType(0);
        template.setSubsidy(0);
    }

    public BossCompanyInfo buildCompany(BaseUser baseUser) {
        BossCompanyInfo info = new BossCompanyInfo();
        info.setBossId(baseUser.getId());
        info.setCompanyName(this.companyName);
        info.setLinkMan(this.linkMan);
        info.setLinkMobile(this.linkMobile);
        info.setAddress(this.address);
        info.setWechatNo(this.wechatNo);
        info.setProvince(this.province);
        info.setCity(this.city);
        info.setRegion(this.region);
        return info;
    }


    private void setPositionParam(BossPosition template, BaseUser user) {
        template.setBossId(user.getId());
        template.setType(type);
        template.setSalaryType(this.salaryType);
        template.setSalary(this.salary);
        template.setJobRequirement(this.jobRequirement);
        template.setJobResponsibility(this.jobResponsibility);
        template.setRemark(this.remark);

        switch (type) {
            case 0:
                setShortTimeJobParam(template);
                break;
            case 1:
                setPartTimeJobParam(template);
                break;
            case 2:
                setFullTimeJobParam(template);
                break;
            default:
                throw new ParamException();
        }
    }
}
